package com.net.tomcat5.Servlet4;

import com.xml.PersonHandler;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

/**
 * Administrator
 *
 * @author jiangbang
 * @date 2016/8/25
 */
public class WebHandler extends DefaultHandler {
    private List<Entity> entityList;
    private List<Mapping> mappingList;
    private Entity entity;
    private Mapping mapping;
    private String beginTag;
    private boolean isMap;




    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        //处理内容
        if(null!=beginTag){
            String str = new String(ch,start,length);
            if(isMap){
                if(beginTag.equals("servlet-name")){
                    mapping.setName(str);
                }else if(beginTag.equals("url-pattern")){
                    mapping.getUrlPattern().add(str);
                }
            }else{
                if(beginTag.equals("servlet-name")){
                    entity.setName(str);
                }else if(beginTag.equals("servlet-class")){
                    entity.setCla(str);
                }
            }
        }
    }

    @Override
    public void endDocument() throws SAXException {
        super.endDocument();
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        //结束元素
        if(null!=qName){
            if(qName.equals("servlet")){
                entityList.add(entity);
            }else if(qName.equals("servlet-mapping")){
                mappingList.add(mapping);
            }
        }
    }

    @Override
    public void startDocument() throws SAXException {
        //文档解析开始

    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        //开始元素
        if(null!=qName){
            beginTag = qName;
            if(qName.equals("servlet")){
                isMap = false;
                entity = new Entity();
            }else if(qName.equals("servlet-mapping")){
                isMap = true;
                mapping = new Mapping();
            }
        }
    }


    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
        //1.获取解析工长
        SAXParserFactory factory = SAXParserFactory.newInstance();
        //2.从解析工厂获取解析器
        SAXParser parser = factory.newSAXParser();
        //3.加载文档 document 注册处理器
        WebHandler web =  new WebHandler();
        //4.编写处理器
       parser.parse(Thread.currentThread().getContextClassLoader().getResourceAsStream("web.xml"),web);
//      parser.parse(new FileInputStream(new File("E:\\git_repo\\demo-git\\src\\main\\java\\com\\net\\tomcat5\\Servlet4\\web.xml")),web);
    }

    public String getBeginTag() {
        return beginTag;
    }

    public void setBeginTag(String beginTag) {
        this.beginTag = beginTag;
    }

    public Entity getEntity() {
        return entity;
    }

    public void setEntity(Entity entity) {
        this.entity = entity;
    }

    public List<Entity> getEntityList() {
        return entityList;
    }

    public void setEntityList(List<Entity> entityList) {
        this.entityList = entityList;
    }

    public boolean isMap() {
        return isMap;
    }

    public void setMap(boolean map) {
        isMap = map;
    }

    public Mapping getMapping() {
        return mapping;
    }

    public void setMapping(Mapping mapping) {
        this.mapping = mapping;
    }

    public List<Mapping> getMappingList() {
        return mappingList;
    }

    public void setMappingList(List<Mapping> mappingList) {
        this.mappingList = mappingList;
    }
}
